/**
 * 
 */
package com.yonyou.iuap.shell.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.ocpsoft.prettytime.PrettyTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.yonyou.iuap.pub.DateUtil;
import com.yonyou.iuap.pub.IDFactory;
import com.yonyou.iuap.shell.dao.KmCommentEntityDao;
import com.yonyou.iuap.shell.entity.KmCommentEntity;
import com.yonyou.iuap.shell.entity.exp.KmCommentBasePoJo;
import com.yonyou.iuap.shell.entity.exp.KmCommentEntityVo;
import com.yonyou.iuap.shell.pub.IShellDaoEnum;
import com.yonyou.iuap.shell.pub.IShellServiceEnum;
import com.yonyou.iuap.shell.pub.RequestDto;
import com.yonyou.iuap.shell.pub.ResultDto;
import com.yonyou.iuap.shell.service.intf.IKmCommentService;

/**
 * @author ganchj 评论增删改查
 */
@Service(IShellServiceEnum.Shell_Service_Comment)
public class KmCommentServiceImpl implements IKmCommentService {

	@Autowired
	@Qualifier(IShellDaoEnum.Shell_Dao_Comment)
	private KmCommentEntityDao<KmCommentEntity> kmCommentEntityDao;

	public ResultDto insert(List<KmCommentEntity> listvo) {
		ResultDto rsDto = new ResultDto();
		rsDto.setStatus(0);
		rsDto.setMessage("添加完成!");
		if (listvo != null && listvo.size() > 0) {
			for (KmCommentEntity vo : listvo) {
				vo.setPk_comment(IDFactory.getInstance().getID());
				vo.setComment_time(DateUtil.formatDateTime(new Date()));
				vo.setDr(0);
				vo.setPk_creator(vo.getPk_user());
				vo.setTs(DateUtil.formatDateTime(new Date()));
				vo.setDtcreate(new Date());
				int i_insert = kmCommentEntityDao.insertSelective(vo);
			}
		}
		return rsDto;
	}

	public ResultDto insert(KmCommentEntity vo) {
		ResultDto rsDto = new ResultDto();
		rsDto.setStatus(0);
		rsDto.setMessage("添加完成!");
		vo.setPk_comment(IDFactory.getInstance().getID());
		vo.setComment_time(DateUtil.formatDateTime(new Date()));
		vo.setDr(0);
		vo.setPk_creator(vo.getPk_user());
		vo.setTs(DateUtil.formatDateTime(new Date()));
		vo.setDtcreate(new Date());
		int i_insert = kmCommentEntityDao.insertSelective(vo);
		return rsDto;
	}

	@Transactional
	public ResultDto update(List<KmCommentEntity> listvo) {
		ResultDto rsDto = new ResultDto();
		rsDto.setStatus(0);
		rsDto.setMessage("更新完成!");
		if (listvo != null && listvo.size() > 0) {
			for (KmCommentEntity vo : listvo) {
				int i_up = kmCommentEntityDao.updateByPrimaryKeySelective(vo);
			}
		}
		return rsDto;
	}

	public ResultDto deleteByCondition(String whereSql) {
		ResultDto rsDto = new ResultDto();
		if (StringUtils.isEmpty(whereSql)) {
			rsDto.setStatus(1);
			rsDto.setMessage("删除条件不能为空!");
		} else {
			int i_del = kmCommentEntityDao.deleteByCondition(whereSql);
			rsDto.setStatus(0);
			rsDto.setMessage("删除完成!");
		}
		return rsDto;
	}

	public ResultDto queryByCondition(String whereSql) {
		ResultDto rsDto = new ResultDto();
		List<KmCommentEntity> listvo = kmCommentEntityDao
				.selectByCondition(whereSql);
		rsDto.setStatus(0);
		rsDto.setMessage("数据查询成功!");
		rsDto.setNum(listvo == null ? 0 : listvo.size());
		rsDto.setCount(listvo == null ? 0 : listvo.size());
		rsDto.setData(listvo);
		return rsDto;
	}

	public ResultDto queryComment(Map param) {
		ResultDto rsDto = new ResultDto();
		if (param != null) {

			if(param.get("pageNum")!=null && param.get("pageSize")!=null){
				int pageNum=Integer.valueOf(param.get("pageNum").toString());
				int pageSize=Integer.valueOf(param.get("pageSize").toString());
				if(pageNum == 0)
					pageNum++;
				if(pageSize == 0)
					pageSize=15;
				pageNum=(pageNum-1) * pageSize;
				param.put("pageNum", pageNum);
				param.put("pageSize", pageSize);
			}
			/*if(param.get("pk_category")!=null){
				StringBuffer sb=new StringBuffer();
				List<String> pklist=(List<String>) param.get("pk_category");
				for(String pk:pklist){
					sb.append("'"+pk+"',");
				}
				sb.deleteCharAt(sb.length()-1);
				param.put("pk_category", sb.toString());
			}*/
			//查所有评论
			if("0".equals(param.get("onlyOne"))){
				//评论回复分开查询
				List<KmCommentBasePoJo> listvo = kmCommentEntityDao
						.queryComment(param);
				List<KmCommentBasePoJo> alist =kmCommentEntityDao.queryAnwser(param);
				listvo.addAll(alist);
				List<KmCommentBasePoJo> clist = format(listvo);
				rsDto.setCount(kmCommentEntityDao.queryCommentCount(param));
				rsDto.setData(clist);
				rsDto.setNum(clist == null ? 0 : clist.size());
				
			}else if("1".equals(param.get("onlyOne"))){
				//只查一条待回复评论
				
				List<KmCommentBasePoJo> listvo = kmCommentEntityDao.queryOneComment(param);
				List<KmCommentBasePoJo> clist = format(listvo);
				rsDto.setData(clist);
			}
			
			
			rsDto.setStatus(0);
			rsDto.setMessage("查询完成!");
			
		} else {
			rsDto.setStatus(1);
			rsDto.setMessage("查询参数不能为空");
		}
		
		return rsDto;
	}

	private List<KmCommentBasePoJo> format(List<KmCommentBasePoJo> listvo) {
		List<KmCommentBasePoJo> clist = new ArrayList<KmCommentBasePoJo>();
		List<KmCommentBasePoJo> alist = new ArrayList<KmCommentBasePoJo>();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:m:s");
		for (KmCommentBasePoJo bean : listvo) {

			if (bean.getComment_type() == 0) {
				// 评论
				clist.add(bean);
			} else if (bean.getComment_type() == 1) {
				// 回复
				alist.add(bean);
			}

			String aa = bean.getComment_time();
			Date date =null ;
			try {
				date = format.parse(aa);
			} catch (ParseException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			Date now = new Date();
			PrettyTime t = new PrettyTime(now);
			bean.setComment_time(t.format(date));
		}
		// 评论添加回复
		for (KmCommentBasePoJo bean : clist) {
			List<KmCommentBasePoJo> slist = new ArrayList<KmCommentBasePoJo>();
			for (KmCommentBasePoJo abean : alist) {
				if (bean.getPk_comment().equals(
						abean.getPk_parent_comment())) {
					slist.add(abean);
				}
			}
			// if(!slist.isEmpty()){
			bean.setData(slist);
			// }
		}
		return clist;
	}

	public List<KmCommentEntityVo> getNotCommentList(RequestDto red) {

		return kmCommentEntityDao.getNotCommentList(red);
	}

	public int getNotCommentCount(RequestDto red) {

		return kmCommentEntityDao.getNotCommentCount(red);
	}

}
